package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Customer;
import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.mapper.CustomerMapper;
import cn.wolfcode.rbac.mapper.DepartmentMapper;
import cn.wolfcode.rbac.mapper.EmployeeMapper;
import cn.wolfcode.rbac.query.CustomerQuery;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.ICustomerService;
import cn.wolfcode.rbac.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional(readOnly = true)
    public PageInfo query(CustomerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Customer> list = this.customerMapper.selectForList(qo);
        for (Customer c : list) {
            if (c.getGender() == 0) {
                c.setDisplayGender("男");
            } else {
                c.setDisplayGender("女");
            }
            if (c.getStatus() == 0) {
                c.setDisplayStatus("潜在客户");
            } else if (c.getStatus() == 1) {
                c.setDisplayStatus("正式客户");
            } else if (c.getStatus() == 2) {
                c.setDisplayStatus("客户池");
            } else if (c.getStatus() == 3) {
                c.setDisplayStatus("失败客户");
            } else if (c.getStatus() == 4) {
                c.setDisplayStatus("流失客户");
            }

        }
        return new PageInfo(list);
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo query0(CustomerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Customer> list = this.customerMapper.selectForList0(qo);
        for (Customer c0 : list) {
            if (c0.getStatus() == 0) {
                c0.setDisplayStatus("潜在客户");
            }
            c0.setSellerName(employeeMapper.selectSellerName(c0.getSellerId()));
        }
        return new PageInfo(list);
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo query1(CustomerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Customer> list = this.customerMapper.selectForList1(qo);
        for (Customer c1 : list) {
            if (c1.getStatus() == 1) {
                c1.setDisplayStatus("正式客户");
            }
        }
        return new PageInfo(list);
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo query2(CustomerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Customer> list = this.customerMapper.selectForList2(qo);
        for (Customer c2 : list) {
            if (c2.getStatus() == 2) {
                c2.setDisplayStatus("客户池");
            }
        }
        return new PageInfo(list);
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo query3(CustomerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Customer> list = this.customerMapper.selectForList3(qo);
        for (Customer c3 : list) {

            if (c3.getStatus() == 3) {
                c3.setDisplayStatus("失败客户");
            }
        }
        return new PageInfo(list);
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo query4(CustomerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Customer> list = this.customerMapper.selectForList4(qo);
        for (Customer c4 : list) {
            if (c4.getStatus() == 4) {
                c4.setDisplayStatus("流失客户");
            }
        }
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Customer customer) {
        if (customer.getId() == null) {
            this.customerMapper.insert(customer);
        } else {
            this.customerMapper.updateByPrimaryKey(customer);
        }
    }

    @Override
    public void delete(Long paramLong) {
        this.customerMapper.deleteByPrimaryKey(paramLong);
    }

    @Override
    @Transactional(readOnly = true)
    public Customer get(Long paramLong) {

        return this.customerMapper.selectByPrimaryKey(paramLong);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Customer> list() {
        List<Customer> customers = this.customerMapper.selectAll();
        return customers;
    }

    @Override
    public void updateStatus(Long customerId, Integer status) {
        this.customerMapper.updateStatus(customerId, status);
    }
}
